package com.one.rope.mvp.web.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Weijian.liu
 * @desc SwaggerUI配置
 * @date 2020-02-19
 */
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Configuration
@Profile({"dev", "test"})
public class SwaggerConfig {

  @Bean
  public Docket businessApi() {
    ParameterBuilder ticketPar = new ParameterBuilder();
    List<Parameter> pars = new ArrayList<Parameter>();
    ticketPar.name("token").description("授权token")
        .modelRef(new ModelRef("string")).parameterType("header")
        .required(false).build(); //header中的token参数非必填，传空也可以
    pars.add(ticketPar.build());
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("OneRopeMVP")
        .genericModelSubstitutes(DeferredResult.class)
        .apiInfo(businessApiInfo())
        .globalOperationParameters(pars)
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.one.rope.mvp.web.controller"))
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo businessApiInfo() {
    return new ApiInfoBuilder()
        .title("多助服务端")
        .description("多助服务端API")
        .termsOfServiceUrl("www.manyhelp.com")
        .contact(new Contact("拉风一哥", "www.lafengyige.com", "709352566@qq.com"))
        .version("1.0.1")
        .build();
  }

}